package cn.arts.assumejson.algo.sort.class01;

import static cn.arts.assumejson.algo.utils.AlgoUtil.swap;

/**
 * Bubble sort.
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2024/7/30 0:03
 */
public class Code02_BubbleSort {

    // Determine the maximum value
    public static void bubbleSort(int[] arr) {
        if (arr == null || arr.length < 2) return;

        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 1; j <= i; j++) {
                if (arr[j] < arr[j - 1]) {
                    swap(arr, j, j - 1);
                }
            }
        }
    }

    // Determine the minimum value
    public static void bubbleSort2(int[] arr) {
        if (arr == null || arr.length < 2) return;

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[i]) {
                    swap(arr, j, i);
                }
            }

        }
    }

}
